package win.larryzeal.spring.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by 张少昆 on 2017/11/20.
 */
@Controller
@RequestMapping( "/demo" )
public class DemoController {
    @RequestMapping( "/test1" )
    public String test1(Map model, HttpServletRequest request){
        request.setAttribute("k1", "v1"); //TODO 看起来只能用一次 req中
        model.put("k2", "v2"); //TODO 看起来只能用一次 req中
        return "test1";
    }

    @RequestMapping( "/test2" )
    public String test2(Map model, HttpServletRequest request){
        Object v1 = request.getAttribute("k1"); //获取不到
        Object v2 = model.get("k2");//获取不到
        System.out.println(v1 + " : " + v2);
        return "test1";
    }

    @RequestMapping( "/test3" )
    public String test3(@RequestAttribute( value = "k1", required = false ) String k1,
                        @RequestAttribute( value = "k1", required = false ) String k2){
        System.out.println(k1 + " : " + k2);
        k1 = "vvvvvvvvvvvv1";
        k2 = "vvvvvvvvvvvv2";
        return "test1";
    }

    @RequestMapping( "/test4" )
    public String test4(Map model, HttpServletRequest request){
        request.setAttribute("k3", "v3"); //TODO 看起来只能用一次 req中
        model.put("k4", "v4"); //TODO 看起来只能用一次 req中
        return "redirect:/demo/test5";
    }

    @RequestMapping( "/test5" )
    public String test5(){
        System.out.println("-----------");
        return "test1";
    }
}
